/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.resource.service;
import com.lczyfz.istep.common.service.CrudService;
import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.modules.resource.dao.ResourceQuestionRepertoryDao;
import com.lczyfz.istep.modules.resource.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 考题题库中间表管理Service
 * @author lsj
 * @version 2017-03-04
 */
@Service
@Transactional(readOnly = true)
public class ResourceQuestionRepertoryService extends CrudService<ResourceQuestionRepertoryDao, ResourceQuestionRepertory> {

	@Autowired
	private ResourceRepertoryService resourceRepertoryService;

	@Autowired
	private ResourceQuestionService resourceQuestionService;


	public ResourceQuestionRepertory get(String id) {
		return super.get(id);
	}
	
	public List<ResourceQuestionRepertory> findList(ResourceQuestionRepertory resourceQuestionRepertory) {
		return super.findList(resourceQuestionRepertory);
	}
	
	public Page<ResourceQuestionRepertory> findPage(Page<ResourceQuestionRepertory> page, ResourceQuestionRepertory resourceQuestionRepertory) {
		return super.findPage(page, resourceQuestionRepertory);
	}

	@Transactional(readOnly = false)
	public void update(String duallist,ResourceRepertory resourceRepertory) {
		ResourceQuestionRepertory temp=new ResourceQuestionRepertory();
		temp.setRepertoryId(resourceRepertory.getId());
		List<ResourceQuestionRepertory> resourceQuestionRepertoryList=super.findList(temp);
		logger.debug("题库-题目中间表数量: [{}]", resourceQuestionRepertoryList.size());


		String[] resourceQuestionList = new String[0];
		if (duallist != null && !duallist.equals(""))
			resourceQuestionList = duallist.split(",");
		logger.debug("length: [{}]", resourceQuestionList.length);


		for (int i = 0; i < resourceQuestionRepertoryList.size(); i++) {
			super.delete(resourceQuestionRepertoryList.get(i));
		}
		resourceRepertory.setQuestionSum(resourceQuestionList.length+"");

		int count=0;
		for(int i=0; i<resourceQuestionList.length;i++)
		{
			ResourceQuestion resourceQuestion = resourceQuestionService.get(resourceQuestionList[i]);
			if(resourceQuestion.getEnvironmentId()!=null && !resourceQuestion.getEnvironmentId().equals(""))
			{
				count++;
			}
		}
		logger.debug("count+++++:＝",count);
		resourceRepertory.setExperimentSum(count+"");
		resourceRepertoryService.save(resourceRepertory);


		for (int i = 0; i < resourceQuestionList.length; i++) {

			ResourceQuestionRepertory resourceQuestionRepertory=new ResourceQuestionRepertory();
			resourceQuestionRepertory.setRepertoryId(resourceRepertory.getId());
			resourceQuestionRepertory.setQuestionId(resourceQuestionList[i]);
			super.save(resourceQuestionRepertory);
		}
	}

	@Transactional(readOnly = false)
	public void save(String duallist,ResourceRepertory resourceRepertory) {
		String[] resourceQuestionList = new String[0];
		int count =0;
		if (duallist != null && !duallist.equals(""))
			resourceQuestionList = duallist.split(",");
		logger.debug("length: [{}]", resourceQuestionList.length);
		resourceRepertory.setQuestionSum(resourceQuestionList.length+"");

		for(int i=0; i<resourceQuestionList.length;i++)
		{
			ResourceQuestion resourceQuestion = resourceQuestionService.get(resourceQuestionList[i]);
			if(resourceQuestion.getEnvironmentId()!=null && !resourceQuestion.getEnvironmentId().equals(""))
			{
				count++;
			}
		}
		resourceRepertory.setExperimentSum(count + "");
		logger.debug("lsj+count{[]}", count);
		resourceRepertoryService.save(resourceRepertory);

		for (int i = 0; i < resourceQuestionList.length; i++) {

			ResourceQuestionRepertory resourceQuestionRepertory =new ResourceQuestionRepertory();
			resourceQuestionRepertory.setRepertoryId(resourceRepertory.getId());
			resourceQuestionRepertory.setQuestionId(resourceQuestionList[i]);


			super.save(resourceQuestionRepertory);
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(ResourceQuestionRepertory resourceQuestionRepertory) {
		super.delete(resourceQuestionRepertory);
	}
	
}